﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 *  MVC网页基类。
 * 
 * 最后修改：2011-10-29
 * ************************************************************/
namespace Mozlite.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Mozlite.Common.Configuration;
    using Mozlite.Common;
    using System.Web.Routing;
    using Mozlite.Web.UI;
    using System.IO;

    /// <summary>
    /// MVC网页基类。
    /// </summary>
    /// <typeparam name="TModel">模型类型。</typeparam>
    /// <typeparam name="TApplication">应用程序类型。</typeparam>
    /// <typeparam name="TConfiguration">应用程序配置类型。</typeparam>
    public abstract class WebBasePage<TModel, TApplication, TConfiguration> : WebViewPage<TModel>
        where TApplication : ApplicationBase<TApplication, TConfiguration>, new()
        where TConfiguration : ConfigurationBase<TConfiguration>, new()
    {
        private string areaName;
        /// <summary>
        /// 获取区域名称。
        /// </summary>
        public string AreaName
        {
            get
            {
                if (areaName == null)
                    areaName = ViewContext.GetAreaName();
                return areaName;
            }
        }

        private string controller;
        /// <summary>
        /// 获取控制器名称。
        /// </summary>
        public string Controller
        {
            get
            {
                if (controller == null)
                    controller = ViewContext.GetController();
                return controller;
            }
        }

        private string action;
        /// <summary>
        /// 获取控制器名称。
        /// </summary>
        public string Action
        {
            get
            {
                if (action == null)
                    action = ViewContext.GetAction();
                return action;
            }
        }

        /// <summary>
        /// 获取绝对路径。
        /// </summary>
        /// <param name="path">文件路径。</param>
        /// <returns>返回绝对路径。</returns>
        protected virtual string GetPath(SystemDirectory directory, string path)
        {
            if(directory == SystemDirectory.Utility)
                return Url.Content(Globals.Combine(directory.ToPath(), path));
            string root = "~/";
            if (AreaName != null)
                root += "Areas/" + this.AreaName + "/";
            if (this.Theme != null)
                root += "Themes/" + this.Theme + "/";
            return Url.Content(Globals.Combine(root + directory.ToPath(), path));
        }

        /// <summary>
        /// 当前模板，如果为模板则必须重写此属性。
        /// </summary>
        public virtual string Theme
        {
            get
            {
                if (AppCurrent.IsThemable)
                    return "default";
                return null;
            }
        }

        /// <summary>
        /// 获取当前应用程序上下文实例。
        /// </summary>
        public AppContext Current
        {
            get
            {
                return AppContext.Current;
            }
        }

        /// <summary>
        /// 获取当前页码。
        /// </summary>
        public int PageIndex
        {
            get
            {
                return ViewContext.RouteData.Get<int>("page", Current.PageIndex);
            }
        }

        /// <summary>
        /// 获取当前配置信息。
        /// </summary>
        public SiteSettings Settings
        {
            get
            {
                return SiteSettings.Current;
            }
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string Local(string key, params object[] args) {
            return Resources.GetString(key, Controller, AreaName, args);
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string Local(object key, params object[] args)
        {
            return Resources.GetString(key, Controller, AreaName, args);
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string GLocal(string key, params object[] args)
        {
            return Resources.GetString(key, null, null, args);
        }

        /// <summary>
        /// 字符串本地化。
        /// </summary>
        /// <param name="key">资源键。</param>
        /// <param name="args">参数。</param>
        /// <returns>返回本地化字符串。</returns>
        protected string GLocal(object key, params object[] args)
        {
            return Resources.GetString(key, null, null, args);
        }

        /// <summary>
        /// 判断当前是否为错误。
        /// </summary>
        protected bool IsValided {
            get {
                return !(ViewBag.__StatusType == false && ViewBag.__Status != null);
            }
        }

        /// <summary>
        /// 获取应用程序当前实例。
        /// </summary>
        protected TApplication AppCurrent
        {
            get
            {
                return ApplicationBase<TApplication, TConfiguration>.Current;
            }
        }

        /// <summary>
        /// 获取当前应用程序配置实例对象。
        /// </summary>
        protected TConfiguration AppConfig
        {
            get
            {
                return AppCurrent.Configuration;
            }
        }

        /// <summary>
        /// 判断当前用户是否通过验证。
        /// </summary>
        protected bool IsAuthenticated {
            get {
                return User.Identity.IsAuthenticated;
            }
        }

        /// <summary>
        /// 获取当前用户。
        /// </summary>
        protected User Self {
            get {
                return Current.User;
            }
        }

        /// <summary>
        /// 获取当前用户ID。
        /// </summary>
        protected int SelfID {
            get {
                return Self.UserID;
            }
        }

        /// <summary>
        /// 获取当前用户名称。
        /// </summary>
        protected string SelfName {
            get {
                return Self.UserName;
            }
        }

        /// <summary>
        /// 获取当前用户昵称。
        /// </summary>
        protected string SelfNickName
        {
            get
            {
                return Self.NickName;
            }
        }

        /// <summary>
        /// 返回当前用户档案实例。
        /// </summary>
        protected UserProfile SelfProfile
        {
            get
            {
                return Current.Profile;
            }
        }

        /// <summary>
        /// 获取文件浏览路径。
        /// </summary>
        /// <param name="fileid">文件ID。</param>
        /// <returns>返回文件浏览路径。</returns>
        protected string GetFileBrowserUrl(int fileid) {
            return GetFileBrowserUrl(fileid);
        }

        /// <summary>
        /// 获取文件浏览路径。
        /// </summary>
        /// <param name="fileid">文件ID。</param>
        /// <returns>返回文件浏览路径。</returns>
        protected string GetFileBrowserUrl(object fileid = null)
        {
            return CommonUrls.GetFileBrowser(fileid.ToInt32(-1));
        }

        /// <summary>
        /// 导入脚本或样式表。
        /// </summary>
        /// <param name="directory">系统文件夹。</param>
        /// <param name="path">文件路径。</param>
        /// <returns>返回HTML代码。</returns>
        protected MvcHtmlString Using(SystemDirectory directory, string path)
        {
            return Using(GetPath(directory, path));
        }

        /// <summary>
        /// 导入脚本或样式表。
        /// </summary>
        /// <param name="path">文件路径，以“~”开头。</param>
        /// <returns>返回HTML代码。</returns>
        protected MvcHtmlString Using(string path)
        {
            return CreateUsing(Url.Content(path));
        }

        private MvcHtmlString CreateUsing(string path)
        {
            if (Globals.GetExtension(path).IsMatched("css"))
                return MvcHtmlString.Create(string.Format("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />", path));
            return MvcHtmlString.Create(string.Format("<script src=\"{0}\" type=\"text/javascript\"></script>", path));
        }

        /// <summary>
        /// 获取根导航。
        /// </summary>
        /// <param name="location">导航位置。</param>
        /// <returns>返回导航实例。</returns>
        public Navigator GetRootNavigator(NavigatorLocation location = NavigatorLocation.Main) {
            if (Theme == null)
                return new Navigator() { SettingsID = Settings.SettingsID, Location = location };
            return new Navigator(Theme) { SettingsID = Settings.SettingsID, Location = location};
        }
    }
}
